<?php

namespace app\api\model;

use think\Model;

class UserPaymentService extends Model
{
    protected $table = 'user_payment_service';



    public function add($orderId, $service)
    {
        $data = $this->params($orderId, $service);
        $this->insertAll($data);
        return array_sum(array_column($data, 'money'));
    }

    private function params($orderId, $service)
    {
        $data = [];
        foreach ($service as $val) {
            $autoService = AutoService::find($val['service_id']);
            $category = AutoCategory::find($autoService['auto_category_id'] ?: 0);
            $modelingService = ModelingServices::find($autoService['service_id'] ?: 0);
            $data[] = [
                'user_payment_id' => $orderId,
                'auto_category_id' => $category['id'] ?: 0,
                'auto_service_id' => $autoService['id'] ?: 0,
                'service_id' => $modelingService['id'] ?: 0,
                'service_name' => $modelingService['service_name'] ?: '',
                'service_price' => $modelingService['service_price'] ?: 0,
                'num' => $val['num'],
                'money' => $modelingService['service_price'] * $val['num'],
                'createtime' => time(),
                'updatetime' => time()
            ];
        }
        return $data;
    }
}
